package com.google.code.timetrail.gui;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.google.code.timetrail.backend.*;

public class StartupFrame extends JPanel{

    /**
     * 
     */
    private static final long serialVersionUID = 3571350411999037117L;
    private Control myGameControl;
    private JLabel gameTitleLabel;
    private JLabel enterNameLabel;
    private JTextField enterNameTextField;
    private JLabel addMembersLabel;
    private JList additionalMembersList;
    private JButton addButton;
    protected JFrame frame;
    protected List<String> additionalPlayers;
    private JList professionList;
    private JLabel professionTextField;
    private JButton nextButton;
    private StartupFrameBackend startupBackend;
    private JPanel myCD;
    public StartupFrame(Control gameControl, JPanel cd){
        this.myGameControl = gameControl;
        this.myCD = cd;
        startupBackend = new StartupFrameBackend(this.myGameControl);
        //contentPane.add(startupPanel, "name_1317839433132283000");
        setLayout(null);
        
        gameTitleLabel = new JLabel(startupBackend.getTitleLabel());
        gameTitleLabel.setBounds(295, 0, 67, 32);
        add(gameTitleLabel);
        
        enterNameLabel = new JLabel(startupBackend.getEnterNameLabel());
        enterNameLabel.setBounds(200, 66, 76, 16);
        add(enterNameLabel);
        
        enterNameTextField = new JTextField();
        enterNameTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
            }
        });
        enterNameTextField.setBounds(295, 60, 134, 28);
        enterNameTextField.setText(startupBackend.getDefaultPlayerName());
        add(enterNameTextField);
        enterNameTextField.setColumns(10);
        
        addMembersLabel = new JLabel(startupBackend.getAddMembersLabel());
        addMembersLabel.setBounds(19, 101, 257, 16);
        add(addMembersLabel);
        
        final DefaultListModel model = new DefaultListModel();
        additionalMembersList = new JList(model);
        additionalMembersList.setBounds(295, 133, 134, 115);
        additionalMembersList.setVisibleRowCount(4);
        add(additionalMembersList);
        
        addButton = new JButton(startupBackend.getAddButtonText());
        addButton.addActionListener(new ActionListener() {
            
            
            

            public void actionPerformed(ActionEvent e) {
                startupBackend.pressAddButton();
                if(startupBackend.canAddPlayer()){
                    model.add(additionalMembersList.getModel().getSize(), startupBackend.getAddedPlayer());
                }

            }
        });
        addButton.setBounds(295, 100, 75, 29);
        add(addButton);
        
        professionList = new JList();
        professionList.addListSelectionListener(new ListSelectionListener() {


            public void valueChanged(ListSelectionEvent e) {
                startupBackend.professionValueChanged(professionList.getSelectedIndex());
                
            }
        });
        professionList.setModel(new AbstractListModel() {
            /**
             * 
             */
            private static final long serialVersionUID = 7908744601807059465L;
            String[] values = startupBackend.getProfessionValues();
            public int getSize() {
                return values.length;
            }
            public Object getElementAt(int index) {
                return values[index];
            }
        });
        professionList.setBounds(295, 260, 134, 76);
        professionList.setVisibleRowCount(4);
        add(professionList);
        
        professionTextField = new JLabel(startupBackend.getProfessionLabelText());
        professionTextField.setBounds(155, 260, 121, 16);
        add(professionTextField);
        
        nextButton = new JButton(startupBackend.getNextButtonText());
        nextButton.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                //CardLayout cd = (CardLayout) contentPane.getLayout();
                

                startupBackend.setNameText(enterNameTextField.getText());
                if(startupBackend.pressNextButton()){
                myCD.add(new RationsPaceFrame(myGameControl, myCD, "Startup"), "name_1317840867491837000");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.next(myCD);
                }
                    //cd.next(contentPane);
                    //selectedProfession - the String selected profession
                    //name = player name, can't be empty or null
                    //additionalPlayers - array that holds the additional players, null if empty
                    
                
            }
        });
        nextButton.setBounds(447, 412, 117, 29);
        add(nextButton);
    }
}
